package com.example.workflow.controller;

import com.example.workflow.annotation.Resubmit;
import com.example.workflow.dto.ApprovalInfo;
import com.example.workflow.dto.CompleteOrder;
import com.example.workflow.dto.DispatcherOrder;
import com.example.workflow.dto.FormDto;
import com.example.workflow.pojo.Form;
import com.example.workflow.pojo.Result;
import com.example.workflow.service.FormService;
import com.example.workflow.vo.FormListVo;
import com.example.workflow.vo.FormVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author
 * @version 1.0
 * @date  2025/4/14
 */
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/order")
public class FormController {
    @Autowired
    private FormService formService;

    /**
     * 新建工单（保存到草稿箱）
     *
     * @param formDto
     * @return
     */
    @PostMapping("/create")
    @PreAuthorize("hasRole('USER')")
    @Resubmit
    public Result<Object> create(@RequestBody FormDto formDto) {
        log.info("创建新草稿");
        formService.create(formDto);
        return Result.success();
    }

    /**
     * 提交（如果已经有，则update，如果未存在，直接提交）
     *
     * @param formDto
     * @return
     */
    @PostMapping("/submit")
    @PreAuthorize("hasRole('USER')")
    @Resubmit
    public Result<Object> submit(@RequestBody FormDto formDto) {
        log.info("提交新工单");
        formService.submit(formDto);
        return Result.success();
    }

    /**
     * 查看个人要处理的审批单子
     *
     * @return 返回工单信息
     */
//    TODO
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('CITY','PROVINCE','USER','OPERATOR','ADMIN')")
    public Result<FormListVo> list(@RequestParam(value = "orderId",required = false)Long id,@RequestParam(value = "type",required = false)String type,@RequestParam(value="status",required = false)String status) {

        List<FormVo> forms = formService.list(id,type,status);
        log.info("查询工单list,{}，{}，{}",id,type,status);
        return Result.success(new FormListVo(forms,forms.size()));
    }

//    /**
//     * 查看个人要操作的单子
//     *
//     * @return
//     */
//    @GetMapping("/process")
//    @PreAuthorize("hasRole('OPERATOR')")
//    public Result<List<Form>> process() {return Result.success(formService.process());}

//    /**
//     * 查看个人需要协助的工单
//     *
//     * @return
//     */
//    @GetMapping("/assist")
//    @PreAuthorize("hasRole('OPERATOR')")
//    public Result<List<Form>> assist() {
//        // TODO 需要协助的工单
//        return Result.success(formService.assist());
//    }

    /**
     * 查询工单详情
     *
     * @param id 工单ID
     * @return 工单信息
     */
    @GetMapping("/details")
    public Result<Form> details(@RequestParam("orderId") Long id) {
        log.info("查询工单详情,{}",id);
        return Result.success(formService.details(id));
    }

    /**
     * 审批
     *
     * @param approvalInfo
     * @return
     */
    @PostMapping("/approve")
    @PreAuthorize("hasAnyRole('CITY','PROVINCE')")
    @Resubmit(key = "#approvalInfo.orderId")
    public Result<Object> approval(@RequestBody ApprovalInfo approvalInfo) {
//        TODO
        log.info("工单审批 ,{}",approvalInfo);
        formService.approval(approvalInfo);
        return Result.success();
    }

//    /**
//     *
//     * 驳回工单
//     * @param orderId
//     * @return
//     */
//    @PostMapping("/reject")
//    @PreAuthorize("hasAnyRole('CITY','PROVINCE')")
//    public Result<Object> reject(@RequestBody Long orderId) {
//        formService.reject(orderId);
//        return Result.success();
//    }

    /**
     * 分派工单
     *
     * @param dispatcherOrder 改写的订单信息
     * @return
     */
    @PostMapping("/distribute")
    @PreAuthorize("hasRole('OPERATOR')")
    @Resubmit(key = "#dispatcherOrder.orderId")
    public Result<Object> dispatcher(@RequestBody DispatcherOrder dispatcherOrder) {
        log.info("派单操作");
        formService.dispatcher(dispatcherOrder);
        return Result.success();
    }


    /**
     * 完成工单
     *
     * @param completeOrder
     * @return
     */
    @PostMapping("/complete")
    @PreAuthorize("hasRole('OPERATOR')")
    @Resubmit(key = "#completeOrder.orderId")
    public Result<Object> complete(@RequestBody CompleteOrder completeOrder) {
        log.info("完成工单");
        formService.complete(completeOrder.getOrderId(),completeOrder.getCompleteLog());
        return Result.success();
    }

    /**
     * 删除草稿工单
     *
     * @param id 要删除的id
     * @return
     */
    @DeleteMapping("/delete")
    @PreAuthorize("hasRole('USER')")
    @Resubmit(key = "#id")
    public Result<Object> delete(@RequestParam Long id) {
        log.info("删除工单id为 {} 的草稿工单",id);
        formService.delete(id);
        return Result.success();
    }
}
